#include "huawei.h"

huawei::huawei()
{
    httpclient_ = std::make_shared<bhttp::client>("https://172.16.149.253");
}

void huawei::start()
{
    login();

    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    fprintf(stdout, "\n\n---------------------------------------\n\n");
    // POST
    refresh();
    search_conferences();
    test_get();
    loginout();
}

void huawei::test_get()
{
    auto req = std::make_shared<bhttp::request>("/conf-portal/addressbook/areas", bhttp::method::GET);
    req->header("Token", token_);
    httpclient_->send(req, std::bind(&huawei::handle_refresh, this, std::placeholders::_1, std::placeholders::_2));
}

void huawei::login()
{
    auto req = std::make_shared<bhttp::request>(itokens, bhttp::method::GET);
    req->auth_type(bhttp::auth::BASIC);
    req->auth_data("5162002:Huawei@123");
    req->parma("clientType", "portal");
    httpclient_->send(req, std::bind(&huawei::handle_login, this, std::placeholders::_1, std::placeholders::_2));
}

void huawei::refresh()
{
    auto req = std::make_shared<bhttp::request>(itokens, bhttp::method::PUT);
    req->parma("grantType", "refresh");
    req->header("Token", token_);
    httpclient_->send(req, std::bind(&huawei::handle_refresh, this, std::placeholders::_1, std::placeholders::_2));
}

void huawei::loginout()
{
    auto req = std::make_shared<bhttp::request>(itokens, bhttp::method::DEL);
    req->header("Token", token_);
    httpclient_->send(req, std::bind(&huawei::handle_loginout, this, std::placeholders::_1, std::placeholders::_2));
}

void huawei::search_conferences()
{
    auto req = std::make_shared<bhttp::request>("/conf-portal/conferences/conditions", bhttp::method::POST);
    req->parma("page", "0");
    req->parma("Size", "20");
    req->header("Token", token_);
    req->header("Content-Type", "application/json");
    req->body(R"({"active": true})");
    httpclient_->send(req, std::bind(&huawei::handle_search_conferences, this, std::placeholders::_1, std::placeholders::_2));
}

void huawei::handle_login(const bhttp::request_ptr& req, const bhttp::response_ptr& rep)
{
    do
    {
        if (!rep->successed())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->error());
            break;
        }
        if (200 != rep->code())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->body_string().c_str());
            break;
        }
        fprintf(stdout, "HTTP Response Body=> \n%s\n", rep->body_string().c_str());

        //"uuid":"ea0ba5225bac091fcc8c5fabde8ac39487d044c465ed63bbbf7624e9c9756af0",
        auto p1 = rep->body_string().find("\"", 0);
        auto p2 = rep->body_string().find("\"", p1+1);
        auto p3 = rep->body_string().find("\"", p2+1);
        auto p4 = rep->body_string().find('\"', p3+1);
        token_ = rep->body_string().substr(p3+1, p4 - p3 - 1);
    } while (false);
}

void huawei::handle_refresh(const bhttp::request_ptr& req, const bhttp::response_ptr& rep)
{
    do
    {
        if (!rep->successed())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->error());
            break;
        }
        if (200 != rep->code())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->body_string().c_str());
            break;
        }
        fprintf(stdout, "HTTP Response Body=> \n%s\n", rep->body_string().c_str());
    } while (false);
}

void huawei::handle_loginout(const bhttp::request_ptr& req, const bhttp::response_ptr& rep)
{
    do
    {
        if (!rep->successed())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->error());
            break;
        }
        if (200 != rep->code())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->body_string().c_str());
            break;
        }
        fprintf(stdout, "HTTP Response Body=> \n%s\n", rep->body_string().c_str());
    } while (false);
}

void huawei::handle_search_conferences(const bhttp::request_ptr& req, const bhttp::response_ptr& rep)
{
    do
    {
        if (!rep->successed())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->error());
            break;
        }
        if (200 != rep->code())
        {
            fprintf(stderr, "请求失败, URL: %s, Error: %s\n", req->path().c_str(), rep->body_string().c_str());
            break;
        }
        fprintf(stdout, "HTTP Response Body=> \n%s\n", rep->body_string().c_str());
    } while (false);
}
